package cn.com.surker.workcon.service.siwei;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.common.Code;
import cn.com.surker.enums.production.indent.ProTypeEnum;
import cn.com.surker.lab.entity.LabExpendUpload;
import cn.com.surker.lab.entity.LabExpendUploadLine;
import cn.com.surker.lab.service.LabExpendUploadLineService;
import cn.com.surker.production.entity.ProductionIndent;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 上海思维任务单
 * @author luotao
 * @date 2025/2/21
 */
public class TaskServiceImpl extends GKJDBCService implements TaskService {

	@Autowired
	private LabExpendUploadLineService labExpendUploadLineService;

	@Override
	public int insert(Long lineId, ProductionIndentVo indent, Date datTim) {
		if (lineId == null || indent == null) {
			return Code.FAILED.getCode();
		}
		//	获取上传的生产线
		List<LabExpendUpload> expendUploadLines = indent.getExpendUploadLines();
		if (expendUploadLines == null || expendUploadLines.isEmpty()) {
			return Code.FAILED.getCode();
		}
		//	拼接任务单数据,对接VTglRwdSync表
		//	任务单编号
		String indentCode = indent.getCode();
		//	客户名称
		String clientName = indent.getClientName();
		//	施工单位
		// String builder = indent.getBuilder();
		//	施工单位联系人
		String linkMan = indent.getLinkMan();
		//	施工单位联系电话
		String linkPhone = indent.getLinkPhone();
		//	合同编号
		String conCode = indent.getConCode();
		//	工程名称
		String conName = indent.getConName();
		//	施工部位
		String site = indent.getSite();
		//	泵送方式
		String castModelName = indent.getCastModelName();
		//	施工地址
		String address = indent.getAddress();
		//	运距
		Double distance = indent.getDistance();
		//	强度等级
		String strengthName = indent.getStrengthName();
		//	塌落度
		String slumps = indent.getSlumps();
		//	备注
		String remark = indent.getRemark();
		//	计划日期
		Date dispatchStartTime = indent.getDispatchStartTime();
		//	计划方量
		Double planStere = indent.getPlanStere();
		//	任务单状态
		String fzt = ProTypeEnum.WORK.getDescription();
		//	砼配合比编号
		String concreteFphbid = "";
		//	砂浆配合比编号
		String standFpbid = "";
		//	搅拌时间
		Integer jbsj = 30;
		//	配合比类型
		String fpblx = "施工";
		//	查询配合比数据
		Long uploadId = null;
		for (LabExpendUpload item : expendUploadLines) {
			Long uploadLine = item.getUploadLine();
			if (uploadLine.equals(lineId)) {
				uploadId = item.getId();
				concreteFphbid = item.getLabLevelCode();
				standFpbid = "R" + concreteFphbid;
				break;
			}
		}
		if (uploadId == null) {
			//	没有配合比
			return -1;
		}
		List<LabExpendUploadLine> uploadMaterials = labExpendUploadLineService.findByPId(uploadId);
		if (uploadMaterials == null || uploadMaterials.isEmpty()) {
			// 配合比下没有材料消耗数据
			return -1;
		}
		//	==========插入逻辑==========
		//	1.任务单插入到工控机分为erp的一条任务单对应工控机的两条,一条为原本任务单(工控机表为trwd,该表中字段:FRwno为erp的字段单号),一条为砂浆任务单,砂浆任务单在原本的任务单后加 "-S" 做区分
		//	2.任务单下为任务单配合比(表:trwdphb)和任务单配合比原材料(表:trwdphbycl)
		SysLine line = getLine(lineId);
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		//	开启事务
		beginTransaction(lineId);
		//	施工备注参数
		String ftbj = remark + "|" + clientName + "|" + linkMan + "|" + linkPhone;
		//	砼参数
		Object[] concreteParams = {conCode, fzt, clientName, conName, site, castModelName, address, distance, dispatchStartTime, strengthName, slumps, planStere, jbsj, indentCode, concreteFphbid, "*", 0, ftbj, "其他"};
		//	砂浆参数
		Object[] standParams = {conCode, fzt, clientName, conName, site, castModelName, address, distance, dispatchStartTime, strengthName, slumps, planStere, jbsj, "R" + indentCode, standFpbid, "*", 0, ftbj, "其他"};
		//	插入砼数据
		insertPhb(lineJdbcTemp, concreteParams, uploadMaterials, datTim, 0);
		//	插入砂浆数据
		insertPhb(lineJdbcTemp, standParams, uploadMaterials, datTim, 1);
		return 1;
	}

	/**
	 * 插入配合比数据
	 *
	 * @param taskParam
	 * @param uploadMaterials
	 */
	private void insertPhb(JdbcTemplate lineJdbcTemp, Object[] taskParam, List<LabExpendUploadLine> uploadMaterials, Date datTim, Integer isStand) {
		String insertRwdSql = "insert into trwd (fhtbh,fzt,fhtdw,fgcmc,fjzbw,fjzfs,fgcdz,fgls,fjhrq,ftpz,ftld,fjhsl,fjbsj,FRWNO,FPhbNo,fscbt,FSGPB,FTbj,frwxz) \n" +
			"values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)\n";

		KeyHolder keyHolder = new GeneratedKeyHolder();
		//	插入任务单数据
		lineJdbcTemp.update(connection -> {
			PreparedStatement ps = connection.prepareStatement(insertRwdSql, new String[]{"FRwdh"});
			for (int i = 0; i < taskParam.length; i++) {
				ps.setObject(i + 1, taskParam[i]);
			}
			return ps;
		}, keyHolder);
		// 获取任务单号
		long frwdh = keyHolder.getKey().longValue();
		//	配合比数据
		String fbz = "";
		//	砂浆配合比原材料数据
		List<Object[]> materials = new ArrayList<>();
		for (LabExpendUploadLine uploadMaterial : uploadMaterials) {
			//	工控机仓位
			String controlStorage = uploadMaterial.getControlStorage();
			//	砼用量
			Double realDosage = uploadMaterial.getRealDosage();
			if (isStand == 1) {
				//	砂浆用量
				realDosage = uploadMaterial.getStandNum();
			}
			//	砂浆用量
			fbz += "{" + controlStorage + "()=" + realDosage + "}";
			//	单个的配合比原材料数据
			Object[] material = {frwdh, controlStorage, realDosage};
			materials.add(material);
		}
		// 2.插入任务单下的配合比数据
		String insertPhbSql = "insert into trwdphb (FRwdh,FTzsj,fpblx,FBz) values (?,?,?,?)\n";
		//	配合比参数
		Object[] phbParams = {frwdh, datTim, "施工", fbz};
		//	插入任务单配合比
		lineJdbcTemp.update(insertPhbSql, phbParams);
		//	3.插入配合比原材料,注意的是插入之前需要删除原来的数据
		//	删除旧数据
		String delYclSql2 = "delete from trwdphbycl where FRwdh = " + frwdh;
		lineJdbcTemp.update(delYclSql2);
		//	新增数据
		String insertYclSql2 = "insert into trwdphbycl (FRwdh,FYlmc,FSysl)\n" +
			"values (?,?,?)\n";
		lineJdbcTemp.batchUpdate(insertYclSql2, materials);
	}
}
